{% extends 'generic/_base.html' %}
{% load helpers %}
{% load buttons %}
{% load perms %}
{% load i18n %}

{% block title %}{{ script }}{% endblock %}

{% block subtitle %}
  {{ script.Meta.description|markdown }}
{% endblock %}

{% block page-header %}
  <div class="container-fluid mt-2">
    <nav class="breadcrumb-container" aria-label="breadcrumb">
      <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="{% url 'extras:script_list' %}">{% trans "Scripts" %}</a></li>
        <li class="breadcrumb-item"><a href="{% url 'extras:script_list' %}#module.{{ script.module }}">{{ script.module|bettertitle }}</a></li>
        <li class="breadcrumb-item"><a href="{{ script.get_absolute_url }}">{{ script }}</a></li>
        <li class="breadcrumb-item">{{ job.created|isodatetime }}</li>
      </ol>
    </nav>
  </div>
{% endblock page-header %}

{% block controls %}
  {% if request.user|can_delete:job %}
    {% delete_button job %}
  {% endif %}
{% endblock controls %}

{% block tabs %}
  <ul class="nav nav-tabs" role="tablist">
    <li class="nav-item" role="presentation">
      <a href="#results" role="tab" data-bs-toggle="tab" class="nav-link active">{% trans "Results" %}</a>
    </li>
  </ul>
{% endblock %}

{% block content %}
    {# Object list tab #}
    <div class="tab-pane show active" id="results" role="tabpanel" aria-labelledby="results-tab">

      {# Object table controls #}
      <div class="d-flex align-items-center mb-3">
        <div>{% trans "Log threshold" %}</div>

        <div class="px-2 d-print-none">
          <div class="dropdown">
            <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
              {{ log_levels|get_key:log_threshold }}
            </button>
            <div class="dropdown-menu">
              {% for level, name in log_levels.items %}
                <a class="dropdown-item d-flex justify-content-between" href="{% url 'extras:script_result' job_pk=job.pk %}?log_threshold={{ level }}">
                  {{ name }}{% if forloop.first %} ({% trans "All" %}){% endif %}
                  {% if level == log_threshold %}<span class="badge bg-green ms-auto"></span>{% endif %}
                </a>
              {% endfor %}
            </div>
          </div>
        </div>

        <div class="ms-auto d-print-none">
          {% if request.user.is_authenticated and job.completed %}
            <div class="table-configure input-group">
              <button type="button" data-bs-toggle="modal" title="{% trans "Configure Table" %}" data-bs-target="#ObjectTable_config"
                class="btn">
                <i class="mdi mdi-cog"></i> {% trans "Configure Table" %}
              </button>
            </div>
          {% endif %}
        </div>
      </div>

      <form method="post" class="form form-horizontal">
        {% csrf_token %}
        {# "Select all" form #}
        {% if table.paginator.num_pages > 1 %}
          <div id="select-all-box" class="d-none card d-print-none">
            <div class="form col-md-12">
              <div class="card-body">
                <div class="form-check">
                  <input type="checkbox" id="select-all" name="_all" class="form-check-input" />
                  <label for="select-all" class="form-check-label">
                    {% blocktrans trimmed with count=table.rows|length object_type_plural=table.data.verbose_name_plural %}
                      Select <strong>all {{ count }} {{ object_type_plural }}</strong> matching query
                    {% endblocktrans %}
                  </label>
                </div>
              </div>
            </div>
          </div>
        {% endif %}

        <div class="form form-horizontal">
          {% csrf_token %}
          <input type="hidden" name="return_url" value="{% if return_url %}{{ return_url }}{% else %}{{ request.path }}{% if request.GET %}?{{ request.GET.urlencode }}{% endif %}{% endif %}" />

          {# Objects table #}
            <div class="col col-md-12"{% if not job.completed %} hx-get="{% url 'extras:script_result' job_pk=job.pk %}" hx-trigger="load delay:0.5s, every 5s"{% endif %}>
              {% include 'extras/htmx/script_result.html' %}
            </div>
          {# /Objects table #}

        </div>
      </form>
    </div>
    {# /Object list tab #}

{% endblock content %}

{% block modals %}
  {% if job.completed %}
  {% table_config_form table table_name="ObjectTable" %}
  {% endif %}
{% endblock modals %}
